package demo.controller;

import demo.entity.Student;
import demo.service.Impl.StudentServiceImpl;
import demo.service.StudentService;

import java.util.Date;
import java.util.Scanner;

public class StudentController {

    private StudentService studentService = new StudentServiceImpl();

    private Scanner scanner = new Scanner(System.in);

    //添加学生
    public Boolean addStudent()
    {
        System.out.print("请输入学生学号:");
        String id = scanner.next();
        if(id.length() != 10)
        {
            System.out.println("学号长度为10位");
            return false;
        }
        if(studentService.exists(id) != -1)
        {
            System.out.println("该学生已存在！！！");
            return false;
        }
        return studentService.add(setStudent(id));
    }

    //更新学生信息
    public Boolean updateStudent()
    {
        String id = ifStudentNumb();
        if (id == null)
            return false;
        return studentService.update(setStudent(id));
    }

    //删除学生信息
    public Boolean deleteStudent()
    {
        String id = ifStudentNumb();
        if (id == null)
            return false;
        return studentService.delete(id);
    }

    //查询单个学生
    public Student selectOneStudent()
    {
        String id = ifStudentNumb();
        if (id == null)
            return null;
        return studentService.selectOne(id);
    }

    //查询所有学生
    public void selectAllStudent()
    {
        System.out.println("学生信息如下：");
        for(Student student : studentService.selectAll())
        {
            System.out.println(student);
        }
    }

    //搜索学生
    public void searchStudent()
    {
        System.out.print("请输入学生姓名关键字");
        String name = scanner.next();

        for(Student student : studentService.selectByName(name))
        {
            System.out.println(student);
        }
    }

    //设置学生出生日期
    private Date setStudentDate()
    {
        System.out.print("请输入年份:");
        int year = scanner.nextInt();
        if (year < 1900 || year > 2100)
        {
            System.out.println("年份范围在1900-2100之间");
            year = scanner.nextInt();
        }
        System.out.print("请输入月份:");
        int month = scanner.nextInt();
        if (month > 12 || month < 1)
        {
            System.out.println("月份范围在1-12之间");
            month = scanner.nextInt();
        }
        System.out.print("请输入日期:");
        int day = scanner.nextInt();
        if (day > 31 || day < 1)
        {
            System.out.println("日期范围在1-31之间");
            day = scanner.nextInt();
        }
        return new Date(year - 1900, month - 1 , day);
    }

    //设置学生信息
    public Student setStudent(String id)
    {
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        System.out.print("请输入学生年龄:");
        Integer age = scanner.nextInt();
        if (age > 100 || age < 0){
            System.out.println("年龄范围在0-100之间");
            age = scanner.nextInt();
        }
        System.out.print("请输入学生成绩:");
        Double score = scanner.nextDouble();
        if (score > 100 || score < 0){
            System.out.println("成绩范围在0-100之间");
            score = scanner.nextDouble();
        }
        System.out.println();
        System.out.println("请输入学生出生日期");
        System.out.println();
        Date date = setStudentDate();
        return new Student(id, name, age, date, score);
    }

    //判断输入的学号时候合法
    private String ifStudentNumb(){
        System.out.print("请输入学生学号:");
        String id = scanner.next();
        if(id.length() != 10)
        {
            System.out.println("学号长度为10位");
            return null;
        }
        if(studentService.exists(id) == -1)
        {
            System.out.println("该学生不存在");
            return null;
        }
        return id;
    }




}
